package com.hksj.Intelligentmanagement.service;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.DicTypeDTO;
import com.hksj.Intelligentmanagement.entity.DicInfoEntity;
import com.hksj.Intelligentmanagement.entity.DicTypeEntity;
import com.hksj.Intelligentmanagement.mapper.DicInfoMapper;
import com.hksj.Intelligentmanagement.mapper.DicTypeMapper;
import com.hksj.Intelligentmanagement.vo.DicTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class DicTypeService {
    @Autowired
    private DicTypeMapper dicTypeMapper;
    @Autowired
    private DicInfoMapper dicInfoMapper;

    //创建字典类型
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result addDicType(DicTypeDTO dicTypeDTO) {
        QueryWrapper<DicTypeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("name", dicTypeDTO.getName());
        Integer count = dicTypeMapper.selectCount(wrapper);
        if (count > 0) {
            return Result.error("字典类型名称已存在");
        }
        //创建一个字典类型实例
        DicTypeEntity dicTypeEntity = new DicTypeEntity();
        //将前端传回的数据复制到dictionaryTypeEntity中
        BeanUtils.copyProperties(dicTypeDTO, dicTypeEntity);
        //在数据库中插入该记录
        int insert = dicTypeMapper.insert(dicTypeEntity);
        if (insert <= 0) {
            return Result.error("新增失败");
        }
        return Result.ok("新增字典类型成功");
    }

    //删除字典类型 那么字典类型下挂的字典明细也全部删除
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result deleteDicType(String id) {
        //在数据库中查询该记录是否存在
        DicTypeEntity dicTypeEntity = dicTypeMapper.selectById(id);
        if (ObjectUtil.isNull(dicTypeEntity)) {
            //数据库没有查到,该记录不存在,id不存在
            return Result.error("字典类型不存在,删除失败");
        }
        //查询出对应父id的子项
        QueryWrapper<DicInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id", id);
        //删除字典明细信息 delete批量删除
        int insert = dicInfoMapper.delete(wrapper);
        //删除字典父项
        int insert1 = dicTypeMapper.deleteById(id);
        //一定有对应的子项吗
        if (insert <= 0 && insert1 <= 0) {
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }

    //更新字典类型
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result updateDicType(DicTypeDTO dicTypeDTO) {
        DicTypeEntity dicTypeEntity = new DicTypeEntity();
        BeanUtils.copyProperties(dicTypeDTO, dicTypeEntity);
        int i = dicTypeMapper.updateById(dicTypeEntity);
        if (i <= 0) {
            return Result.error("更新失败");
        }
        return Result.ok("更新成功");
    }

    //根据id查询字典类型信息
    public Result queryDicTypeById(String id) {
        //调用selectById查询专业信息
        DicTypeEntity entity = dicTypeMapper.selectById(id);
        //返回查询结果
        return Result.data(entity);
    }

    //查询字典类型列表
    public List<DicTypeVo> queryDicTypeList() {
        //调用mapper函数查询字典类型列表
        return dicTypeMapper.queryDicTypeList();
    }

}
